1. Field
The present invention relates to the computer field, particularly to runtime memory analysis, and more particularly, to a method and system for facilitating runtime memory analysis.
2. Description of the Related Art
To check potential runtime memory problems in a program, the program will usually be instrumented to track memory access events when the program runs. That is to say, some extra statements or instructions are inserted into the program, such that when the program is running, these extra statements or instructions are executed along with the original program instructions. The inserted statements or instructions will invoke a routine in a “memory access tracking” module, and the routine in the “memory access tracking” module will record memory access events in storage (e.g., a list and database in a memory).
Program instrumentation has been widely used in analysis tools (e.g., dynamic memory analysis tools), optimization tools and testing tools. These tools use program instrumentation to modify binary code or byte code, to achieve such goals as performance analysis, code optimization and coverage testing analysis.
FIG. 1 depicts a schematic diagram of program instrumentation for memory access tracking. As shown, the program runs in a runtime environment after being instrumented, and invokes a memory access tracking module via the inserted extra code during the running. The memory access tracking module stores memory access events into a storage, and loads the stored memory access events from the storage to perform relevant analysis.
For example, as to the following simple Java program:
class Simple{int i;public void set (int i) {this.i = i}},some of the statements therein are instrumented so as to record memory access operations. The instrumented program is shown as follows (actually, all instrumentations are performed on binary code or byte code, instead of source code, and the program is shown only to illustrate how code is modified in instrumentation):
class Simple{int i;public void set (int i){this.i = i;runtimeRecorder.addMemAccess(this, “i”, Constants.WRITE,Thread.getId( ));}}
Thus during the running of the program, when the program writes a new value to field i of class Simple, the method addMemAccess will be invoked. The method addMemAccess has 4 parameters, including the memory access object reference, field name, access type and current thread ID. In this way, the method will record a memory access event including the above information in the memory, and can perform dynamic memory analysis on the memory access events that have been recorded.
A thread is a kind of independent, schedulable flow of instructions, and is a logical flow that runs in the context of a process. Threads are an increasingly important programming model because of the requirement for concurrent software by multi-core processors today. Although a thread is a much more lightweight execution unit than a process, frequent creation and termination of threads is still expensive. To reduce thread creations and terminations, modern system allows a thread to fetch and execute tasks continuously in its lifetime.
A task is a stand-alone job that represents an instruction sequence and is completely independent of other jobs. A thread can actually contain multiple tasks, each running in the context of the thread. After one task finishes its execution in the thread, the thread fetches another task ready to be executed. Tasks are usually executed sequentially in a thread without interleaving. A typical model of thread/task is a thread pool and working queue, which are widely used in industrial software to improve system throughput and performance.
Since dynamic memory analysis tools should record every memory access event, and as program runs, more and more memory access events are recorded in storage (e.g., memory), the memory overhead will become larger and larger, and in the meantime, dynamic memory analysis will become slower and slower.
FIG. 2 shows a comparison of memory usages of two exemplary applications before and after using program instrumentation to perform dynamic memory analysis. As shown, for the WAS Trade application, the memory overhead of dynamic memory analysis is 3.3×; while for the Tomcat application, the memory overhead of dynamic memory analysis varies from 3× to 5×. It can be seen that tracking memory access in dynamic memory analysis will bring about huge/tremendous extra memory consumption.
Therefore, a method for reducing memory overhead in runtime memory analysis so as to make the runtime memory analysis more efficient is needed in the art.